package com.spzx.channel.service.impl;

import com.spzx.channel.service.IndexService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.CategoryVo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Haoran Sun
 * @since 2024/07/23
 */

@Service
public class IndexServiceImpl implements IndexService {

    private static final Logger log = LoggerFactory.getLogger(IndexServiceImpl.class);
    @Resource
    private RemoteCategoryService remoteCategoryService;
    @Resource
    private RemoteProductService remoteProductService;

    @Override
    public Map<String, Object> getIndexData() {

        HashMap<String, Object> resultMap = new HashMap<>();

        R<List<CategoryVo>> categoryVos = remoteCategoryService.getOneCategory(SecurityConstants.INNER);
        if (categoryVos.getCode() == R.FAIL) {
            log.error("远程调用 失败{}", categoryVos.getMsg());
            throw new ServiceException(categoryVos.getMsg());
        }
        resultMap.put("categoryList",categoryVos.getData());

        R<List<ProductSku>> topSales = remoteProductService.getTopSale(SecurityConstants.INNER);
        if(topSales.getCode() == R.FAIL){
            log.error("远程调用 失败{}", topSales.getMsg());
            throw new ServiceException(topSales.getMsg());
        }
        resultMap.put("productSkuList",topSales.getData());
        return resultMap;
    }
}
